'use strict';
const Server = require('egg').Service;
class UserAccessService extends Server {
  async login(payload) {
    const { ctx, service } = this;
    const user = await service.user.findByMobile(payload.mobile);
    console.log(user);

    if (!user) {
      ctx.throw(404, 'user not fond');
    }
    const verifyPsw = await ctx.compare(payload.password, user.password);
    if (!verifyPsw) {
      ctx.throw(404, 'user password is error');
    }
    // 生成令牌
    return {
      token: await service.actionToken.apply(user._id),
    };


  }
  async logout() {
  }
  async current() {
    const { ctx, service } = this;
    const _id = ctx.state.user.data._id;
    const user = await service.user.find(_id);
    if (!user) {
      ctx.throw(404, 'user is not fond');
    }
    user.password = '';
    return user;

  }

}
module.exports = UserAccessService;
